package top.zhangjianyong.tools.service.api.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import top.zhangjianyong.tools.entity.stock.StockUser;
import top.zhangjianyong.tools.mapper.StockUserMapper;
import top.zhangjianyong.tools.parm.stock.*;
import top.zhangjianyong.tools.service.api.StockUserService;
import top.zhangjianyong.tools.user.UserHolder;
import top.zhangjianyong.tools.utils.ApiResponse;

import java.util.List;

/**
 * @author zhangjianyong
 * @since 2025/3/24 下午2:00
 */
@Service
public class StockUserServiceImpl
        extends ServiceImpl<StockUserMapper, StockUser> implements StockUserService {

    @Override
    public ApiResponse login(StockUserLoginParam param) throws Exception {
        StockUser one = lambdaQuery().eq(StockUser::getUsername, param.getUsername())
                .one();
        if (one == null) {
            return ApiResponse.fail("用户不存在");
        }
        if (!one.getPassword().equals(param.getPassword())) {
            return ApiResponse.fail("密码错误");
        }
        if (0 == one.getState()) {
            return ApiResponse.fail("未激活，联系管理员激活");
        }
        UserHolder.user(one);
        return ApiResponse.success(one);
    }

    @Override
    public ApiResponse reg(StockUserRegParam param) throws Exception {
        StockUser one = lambdaQuery().eq(StockUser::getUsername, param.getUsername())
                .one();
        if (one != null) {
            return ApiResponse.fail("用户已存在");
        }
        StockUser stockUser = new StockUser();
        BeanUtils.copyProperties(param, stockUser);
        stockUser.setState(0);
        stockUser.setAdmin(0);
        stockUser.setDelFlag(0);
        boolean save = save(stockUser);
        if (save) {
            //UserHolder.user(stockUser);
            return ApiResponse.success();
        } else {
            return ApiResponse.fail("注册失败");
        }
    }

    @Override
    public ApiResponse approve(StockUserApproveParam param) {
        if (lambdaUpdate()
                .eq(StockUser::getId, param.getId())
                .set(StockUser::getState, param.getState())
                .update()) {
            return ApiResponse.success();
        }
        return ApiResponse.fail("更新失败");
    }

    @Override
    public ApiResponse list(StockUserListParam param) {
        String keyword = param.getKeyword();
        List<StockUser> list = lambdaQuery()
                .like(StrUtil.isNotBlank(keyword), StockUser::getName, keyword)
                .or(StrUtil.isNotBlank(keyword))
                .like(StrUtil.isNotBlank(keyword), StockUser::getUsername, keyword)
                .or(StrUtil.isNotBlank(keyword))
                .like(StrUtil.isNotBlank(keyword), StockUser::getPhone, keyword)
                .list();
        return ApiResponse.success(list);
    }

    @Override
    public ApiResponse update(StockUserUpdateParam param) {
        StockUser one = getById(param.getId());
        if (one == null) return ApiResponse.fail("用户不存在");
        StockUser user = new StockUser();
        BeanUtils.copyProperties(param, user);
        return updateById(user) ? ApiResponse.success() : ApiResponse.fail("更新失败");
    }
}
